<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

<script type="text/javascript">
    var rowId = 0;
    
    $('.ui-reset , #refresh_matrixcategoryGrid').click(function(){
        $("#matrixcategoryGrid").jqGrid('setGridParam',{search:false});
        var postData = $("#matrixcategoryGrid").jqGrid('getGridParam','postData');
        $.extend(postData,{searchField:"",searchString:"",searchOper:""});
        $.extend(postData,{filters:""});
        $("#matrixcategoryGrid").trigger("reloadGrid");
    });

    $.subscribe('matrixcategoryGridRowSelected', function(event,data) {
        rowId = event.originalEvent.id;
        $( "#matrixcategoryGrid_edit" ).button( "option", "disabled", false );
        $( "#matrixcategoryGrid_delete" ).button( "option", "disabled", false );
    });

    $.subscribe('searchMatrixcategoryGrid', function(event,data) {
        $("#matrixcategoryGrid").jqGrid('searchGrid', {drag:true,closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:"Search Employee"} );
    });

    $.subscribe('addMatrixcategoryGrid', function(event,data) {
        $("#matrixcategoryGrid").jqGrid('editGridRow',"new",{drag:true,closeAfterEdit:false,closeOnEscape:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,position:"last",afterSubmit:function(response, postdata) {return isError(response.responseText);}});
    });

    $.subscribe('editMatrixcategoryGrid', function(event,data) {
        if(rowId!=0){
            $("#matrixcategoryGrid").jqGrid('editGridRow',rowId,{drag:true,closeAfterEdit:true,closeOnEscape:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,afterSubmit:function(response, postdata) {return isError(response.responseText);}});
        }else{
            alert("Sorry. Please select an category first.");
        }
    });

    $.subscribe('deleteMatrixcategoryGrid',function(event,data){
        if(rowId!=0){
            $("#matrixcategoryGrid").jqGrid('delGridRow',rowId,{drag:true,closeOnEscape:true,closeAfterEdit:true,jqModal:true,left:310,top:20,reloadAfterSubmit:true});
        }else{
            alert("Sorry. Please select an category first.");
        }
    });
    
    $.subscribe('matrixcategoryGridComplete',function(event,data){
        $( "#matrixcategoryGrid_search" ).button( "option", "disabled", false );
        $( "#matrixcategoryGrid_add" ).button( "option", "disabled", false );
    });
    
    function isError(text) {
        if(text.indexOf('ERROR') >= 0) { return [false, text]; }
        return [true,'Done.'];
    }
</script>
<div class="gridOptsContainer">
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="matrixcategoryGrid_search" onClickTopics="searchMatrixcategoryGrid" value="Search Category"/>
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="matrixcategoryGrid_add" onClickTopics="addMatrixcategoryGrid" value="Add Category"/>
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="matrixcategoryGrid_edit" onClickTopics="editMatrixcategoryGrid" value="Edit Category"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="matrixcategoryGrid_delete" onClickTopics="deleteMatrixcategoryGrid" value="Delete Category"/>
</div>
<s:url id="remoteurl" action="grid-matrixcategory"/>
<s:url id="editurl" action="edit-matrixcategory"/>
<sjg:grid
    id="matrixcategoryGrid"
    caption="Cashmate Matrix Categories"
    dataType="json"
    href="%{remoteurl}"
    editurl="%{editurl}"
    gridModel="gridModel"
    pager="true"
    pagerButtons="false"
    pagerInput="false"
    rowNum="-1"
    loadingText="Requesting Data..."
    width="970"
    shrinkToFit="true"
    hoverrows="true"
    rownumbers="true"
    autoencode="true"
    viewrecords="true"
    navigator="true"
    navigatorAdd="false"
    navigatorDelete="false"
    navigatorEdit="false"
    navigatorSearch="false"
    navigatorView="true"
    onSelectRowTopics="matrixcategoryGridRowSelected"
    onGridCompleteTopics="matrixcategoryGridComplete"
    >

    <sjg:gridColumn name="matrixcategid"
                    key="true"
                    index="matrixcategid"
                    title="matrixcategid (Not Editable)"
                    sortable="false"
                    hidden="true"
                    />

    <sjg:gridColumn name="categname"
                    index="categname"
                    title="Category"
                    sortable="true"
                    editable="true"
                    edittype="text"
                    search="true"
                    editrules="{required:true}"
                    searchoptions="{sopt:['eq']}"
                    editoptions="{maxlength:'45'}"
                    align="center"
                    />

    <sjg:gridColumn name="percentage"
                    index="percentage"
                    title="Percentage"
                    sortable="true"
                    editoptions="{maxlength:'3'}"
                    editrules="{required:true,number:true}"
                    editable="true"
                    edittype="text"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    align="center"
                    />
</sjg:grid>
